package data.factory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import data.vectors.DoubleVector;
import data.vectors.implementation.DoubleVector_Sparse_Memory;

public class OctaveLoader {

	private String filename;

	public OctaveLoader(String filename) {
		super();
		this.filename = filename;
	}

	public DoubleVector loadDoubleVectorSparse(){
		DoubleVector_Sparse_Memory vec = new DoubleVector_Sparse_Memory();

		//filename = path+"/labels.txt";
		FileReader fr;
		try {
			fr = new FileReader(new File(filename));

			BufferedReader in = new BufferedReader(fr);
			String str; 

			while(true){
				str = in.readLine();
				if (!str.startsWith("#"))
					break;
			}
			// toujours un vecteur de 2 valeurs ID, valeur
			while(str!=null){
				String[] ss =  str.split("[\\s]+");
				int id = Integer.valueOf(ss[0]);
				double value = Double.valueOf(ss[1]);

				vec.setValue(id, value);

				str = in.readLine();
			}
			in.close();
			return vec;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public int[][] loadIntMat() {
		FileReader fr;
		try {
			fr = new FileReader(new File(filename));

			BufferedReader in = new BufferedReader(fr);
			String str; 
			int nrows=0,ncolumns=0;
			while(true){
				str = in.readLine();
				int begin = str.indexOf("rows");
				if (begin>0 && begin<6)
					nrows = Integer.valueOf(str.substring(begin+6));
				begin = str.indexOf("columns");
				if (begin>0 && begin<6)
					ncolumns = Integer.valueOf(str.substring(begin+9));

				if (!str.startsWith("#"))
					break;
			}
			
			int[][] result = new int[nrows][ncolumns];
			// toujours un vecteur de 2 valeurs ID, valeur
			int i=0;
			while(str!=null){
				String[] ss =  str.split("[\\s]+");
				for(int j=0; j<ncolumns; j++){
					result[i][j] = Integer.valueOf(ss[j]);
				}
				str = in.readLine();i++;
			}
			in.close();
			return result;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public int[] loadIntVec() {
		FileReader fr;
		try {
			fr = new FileReader(new File(filename));

			BufferedReader in = new BufferedReader(fr);
			String str; 
			int nrows=0,ncolumns=0;
			while(true){
				str = in.readLine();
				int begin = str.indexOf("rows");
				if (begin>0 && begin<6)
					nrows = Integer.valueOf(str.substring(begin+6));
				begin = str.indexOf("columns");
				if (begin>0 && begin<6)
					ncolumns = Integer.valueOf(str.substring(begin+9));

				if (!str.startsWith("#"))
					break;
			}
			
			int[] result = new int[nrows*ncolumns];
			// toujours un vecteur de 2 valeurs ID, valeur
			int cpt=0;
			while(str!=null){
				String[] ss =  str.split("[\\s]+");
				for(int j=0; j<ss.length; j++){
					result[cpt] = Integer.valueOf(ss[j]);cpt++;
				}
				str = in.readLine();
			}
			in.close();
			return result;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public double[] loadDoubleVec() {
		FileReader fr;
		try {
			fr = new FileReader(new File(filename));

			BufferedReader in = new BufferedReader(fr);
			String str; 
			int nrows=0,ncolumns=0;
			while(true){
				str = in.readLine();
				int begin = str.indexOf("rows");
				if (begin>0 && begin<6)
					nrows = Integer.valueOf(str.substring(begin+6));
				begin = str.indexOf("columns");
				if (begin>0 && begin<6)
					ncolumns = Integer.valueOf(str.substring(begin+9));

				if (!str.startsWith("#"))
					break;
			}
			
			double[] result = new double[nrows*ncolumns];
			// toujours un vecteur de 2 valeurs ID, valeur
			int cpt=0;
			while(str!=null){
				String[] ss =  str.split("[\\s]+");
				for(int j=0; j<ss.length; j++){
					result[cpt] = Double.valueOf(ss[j]);cpt++;
				}
				str = in.readLine();
			}
			in.close();
			return result;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Double[][] loadDoubleMat() {
		FileReader fr;
		try {
			fr = new FileReader(new File(filename));

			BufferedReader in = new BufferedReader(fr);
			String str; 
			int nrows=0,ncolumns=0;
			while(true){
				str = in.readLine();
				int begin = str.indexOf("rows");
				if (begin>0 && begin<6)
					nrows = Integer.valueOf(str.substring(begin+6));
				begin = str.indexOf("columns");
				if (begin>0 && begin<6)
					ncolumns = Integer.valueOf(str.substring(begin+9));

				if (!str.startsWith("#"))
					break;
			}
			
			Double[][] result = new Double[nrows][ncolumns];
			// toujours un vecteur de 2 valeurs ID, valeur
			int i=0;
			while(str!=null){
				String[] ss =  str.split("[\\s]+");
				for(int j=0; j<ncolumns; j++){
					result[i][j] = Double.valueOf(ss[j]);
				}
				str = in.readLine();i++;
			}
			in.close();
			return result;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
